package com.xzp.blog.service.impl;

import com.xzp.blog.entity.Article;
import com.xzp.blog.entity.dto.form.ArticleSearchForm;
import com.xzp.blog.entity.vo.PostView;
import com.xzp.blog.mapper.ArticleMapper;
import com.xzp.blog.service.BaseViewTransableService;
import com.xzp.blog.service.IPostsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 博客业务实现类
 */
@Service
public class PostServiceImpl extends BaseViewTransableService<Article,PostView> implements IPostsService {


    @Autowired
    ArticleMapper articleMapper;


    @Override
    public List<PostView> getPostList() {
        List<Article> articles = articleMapper.getPostViewAllArticles();
        List<PostView> postViews = transEntityToView(articles);
        return postViews;
    }

    @Override
    @Deprecated  //暂时废弃
    public List<PostView> getPostListByDate(Date start, Date end) {
        return null;
    }

    @Override
    public List<PostView> getPostListByTagId(Integer tagId) {
        List<Article> articles = articleMapper.getArticleListByTagId(tagId);
        List<PostView> postViews = transEntityToView(articles);
        return postViews;

    }

    @Override
    public List<PostView> getPostListByArticleCondition(ArticleSearchForm form) {

        Article article = new Article();
        article.setTitle(form.getName());
        List<Article> articles = articleMapper.getArticleListByCondition(form);
        return transEntityToView(articles);
    }


    @Override
    protected List<PostView> transEntityToView(List<Article> entityList) {
        List<PostView> postViewsList = new ArrayList<>();
        Iterator it = entityList.iterator();
        while (it.hasNext()) {
            Article article = (Article) it.next();
            PostView postView = new PostView(article);
            postViewsList.add(postView);
        }
        return postViewsList;
    }
}
